// pages/linear/linear.js
import * as echarts from '../../ec-canvas/echarts';
var utils = require('../../utils/util.js')

function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // 像素
  });
  canvas.setChart(chart);

  var option = {
    color: ['#37a2da', '#32c5e9', '#67e0e3'],
    tooltip: {
      trigger: 'axis',
      axisPointer: { // 坐标轴指示器，坐标轴触发有效
        type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
      },
      confine: true
    },
    legend: {
      data: ['热度', '正面', '负面']
    },
    grid: {
      left: 20,
      right: 20,
      bottom: 15,
      top: 40,
      containLabel: true
    },
    xAxis: [{
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#999'
        }
      },
      axisLabel: {
        color: '#666'
      }
    }],
    yAxis: [{
      type: 'category',
      axisTick: {
        show: false
      },
      data: ['汽车之家', '今日头条', '百度贴吧', '一点资讯', '微信', '微博', '知乎'],
      axisLine: {
        lineStyle: {
          color: '#999'
        }
      },
      axisLabel: {
        color: '#666'
      }
    }],
    series: [{
        name: '热度',
        type: 'bar',
        label: {
          normal: {
            show: true,
            position: 'inside'
          }
        },
        data: [300, 270, 340, 344, 300, 320, 310],
        itemStyle: {
          // emphasis: {
          //   color: '#37a2da'
          // }
        }
      },
      {
        name: '正面',
        type: 'bar',
        stack: '总量',
        label: {
          normal: {
            show: true
          }
        },
        data: [120, 102, 141, 174, 190, 250, 220],
        itemStyle: {
          // emphasis: {
          //   color: '#32c5e9'
          // }
        }
      },
      {
        name: '负面',
        type: 'bar',
        stack: '总量',
        label: {
          normal: {
            show: true,
            position: 'left'
          }
        },
        data: [-20, -32, -21, -34, -90, -130, -110],
        itemStyle: {
          // emphasis: {
          //   color: '#67e0e3'
          // }
        }
      }
    ]
  };
  chart.setOption(option);
  return chart;
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      onInit: initChart
    },
    k: '',
    b: '',
    equation: 'x',
    retFenzi: 0,
    retFenmu: 0,
    showRet: false,
    crossPointX: {
      fenzi: 0,
      fenmu: 0
    },
    crossPointY: {
      fenzi: 0,
      fenmu: 0
    },
    isRetMinus: false
  },

  // 函数输入框值改变事件
  inputK: function(e) {
    let val = e.detail.value
    let isNum = utils.judgeNum(val)
    if (val === '' || isNum) {
      this.setData({
        k: e.detail.value
      })
      this.buildEquation()
    } else {
      this.setData({
        k: this.data.k
      })
    }
  },

  //一次函数b值输入框值改变事件
  inputB: function(e) {
    let val = e.detail.value
    let isNum = utils.judgeNum(val)
    if (val === '' || isNum) {
      this.setData({
        b: e.detail.value
      })
      this.buildEquation()
    } else {
      this.setData({
        b: this.data.b
      })
    }
  },

  // 构建函数式子
  buildEquation: function() {
    let equation = ''
    if (this.data.k === '1') {
      equation += 'x'
    } else if (this.data.k === '-1') {
      equation += '-x'
    } else {
      equation += this.data.k + 'x'
    }
    if (parseFloat(this.data.b) > 0) {
      equation += '+' + this.data.b
    } else if (parseFloat(this.data.b) < 0) {
      equation += this.data.b
    }
    this.setData({
      equation: equation
    })
  },

  // 计算按钮点击事件
  count: function() {
    let k, b
    this.data.k === '' ? k = 1 : k = parseFloat(this.data.k)
    this.data.b === '' ? b = 0 : b = parseFloat(this.data.b)
    console.log('传入的被除数和除数是', k, b)
    let shang = utils.getShang(b, k)
    console.log('得到的商是')
    this.setData({
      crossPointX: shang,
      crossPointY: {
        fenzi: b
      },
      showRet: true,
      isRetMinus: shang.isRetMinus
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})